﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;


namespace LiveProjectManager.DDD.Services
{
    public class ResourceService : Base.Service<Domains.Resource>
    {
        public ResourceService() : base()
        {
        }


        override public ICollection<Domains.Resource> getAll(int paginateBy = 0, int page = 1)
        {
            if (paginateBy > 0)
            {
                if (page <= 0)
                    page = 1;
                return (from r in _repository orderby r.Id select r).Skip(paginateBy * (page - 1)).Take(paginateBy).ToList();
            }
            return (from r in _repository orderby r.Id select r).ToList();
        }


        public ICollection<Domains.Resource> getAllByCategory(Domains.Category myCategory)
        {
            return (from r in _repository where r.Category.Id == myCategory.Id select r).ToList();
        }
        

        public Domains.Resource getByLabel(string myLabel)
        {
            ICollection<Domains.Resource> myResults = (
                from r in _repository
                where r.Label == myLabel
                select r
            ).ToList();
            return (myResults.Count != 1) ? null : myResults.First();
        }


        public bool exists(string myLabel)
        {
            return (getByLabel(myLabel) != null);
        }


        public ICollection<Domains.Resource> getAllByUser(Domains.User myUser)
		{
            return (from r in _repository where r.User.Id == myUser.Id select r).ToList();
        }


        override public Domains.Resource getById(long myId)
		{
            ICollection<Domains.Resource> myResults = (from r in _repository
                                                             where r.Id == myId
                                                             select r).ToList();
            return (myResults.Count != 1) ? null : myResults.First();
        }


        public bool create(
            string myLabel,
            Domains.Category myCategory,
            Domains.User myUser,
            Domains.File myFile,
            string myDescription = null,
            bool autoSave = true
        )
        {
            try
            {
                // Resource already exists.
                if (exists(myLabel))
                    return false;

                // Create the new user.
                Domains.Resource myNewResource = new Domains.Resource
                {
                    Label = myLabel,
                    File = myFile,
                    User = myUser,
                    Category = myCategory,
                    CreatedOn = DateTime.Now
                };
                if (myDescription != null)
                    myNewResource.Description = myDescription;

                // Insert in repository.
                _repository.Add(myNewResource);
                if (autoSave)
                    _context.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        override public bool remove(long myId, bool autoSave = true)
        {
            try
            {
                Domains.Resource myResource = getById(myId);
                if (myResource == null)
                    return false;
                _repository.Remove(myResource);
                if (autoSave)
                    _context.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}